﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DingTalkAgent.Web.Controllers
{
    public class AccountController : Controller
    {

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        public ActionResult LogOn()
        {

            return View();
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult LogOn(FormCollection forms)
        {


            string code = "";
            #region 获取值
            try
            {
                code = Session["CheckCode"].ToString();//读取验证码
                Session.Remove("CheckCode");
            }
            catch
            {
                return Content("<script>window.parent.alertMsg('验证码过期或是已经禁用cookies！！');</script>");
            }
            string type = forms["type"].ToString();
            string UserCode = forms["UserCode"].ToString();
            string Password = forms["Password"].ToString();
            string CheckCode = forms["Safe"].ToString().ToUpper();
            if (CheckCode.ToLower() != code.ToLower())
            {
                return Content("<script>window.parent.alertMsg('输入的验证码无效，请重新输入！！');</script>");
            }


            #endregion

            if (string.IsNullOrEmpty(UserCode) || string.IsNullOrEmpty(Password))
            {

                return Content("<script>window.parent.alertMsg('必需填写所有项！！');</script>");
            }

            SqlData.Admin_Info danhandel = new SqlData.Admin_Info();
            var m = danhandel.Logon(UserCode.Trim(), Password.Trim());
            if (m == null)
            {
                return Content("<script>window.parent.alertMsg('账号或密码不正确！！');</script>");

            }

            if (m.status > 0)
            {
                Session["admin"] = m;
                Session["RoleID"] = m.RoleID;
                return Content("<script>window.parent.alertMsg('1');</script>");
            }
            else
            {
                return Content("<script>window.parent.alertMsg('账号已经暂停，不可以登录！！');</script>");
            }


        }
        //登出
        public ActionResult LogOut()
        {
            if (Session["admin"] != null)
            {
                Session.Remove("admin");
            }
            if (Session["driver"] != null)
            {
                Session.Remove("driver");
            }

            //操作记录
            return RedirectToAction("LogOn");
        }
    }
}
